package com.woniuxy.usercoachassemble.commons;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/**
 * ClassName: YZMUtils
 * Description: 验证码图片生成工具类
 * date: 2020/7/13 14:28
 *
 * @author Fy浪迹、
 */
public class YZMUtils {
    //得到随机数
    private static char[] chars = "abdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY3456789".toCharArray();
    private static Random random = new Random();
    private static String getCode(int length){
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<length;i++){
            sb.append(chars[random.nextInt(chars.length)]);
        }
        return sb.toString();
    }

    /*
     * @说明: 设计验证码并写出图片文件
     */
    public static void createCode(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //1.创建画布
            BufferedImage bufferedImage = new BufferedImage(120, 34, BufferedImage.TYPE_INT_RGB);
            //2.创建画笔
            Graphics2D graphics2d = bufferedImage.createGraphics();
            //6.设置背景颜色  ， 先设置画笔颜色  ，在把画布涂满
            graphics2d.setColor(new Color(249, 250, 108));
            //7.涂满画布
            graphics2d.fillRect(0, 0, 120, 34);
            //8.更新画笔颜色
            graphics2d.setColor(new Color(62, 128, 27));
            //9.设置字体
            graphics2d.setFont(new Font("Dope Crisis", Font.PLAIN, 40));
            String code = getCode(4);
            //12.保存到sesssion中
            HttpSession session=req.getSession();
            session.setAttribute(ConstantPool.IMG_YZM_CODE, code);
            //设置失效时间2分钟
            session.setMaxInactiveInterval(120);
            //3.写字
            graphics2d.drawString(code, 25,30);
            //4.收笔
            graphics2d.dispose();

            //5.保存到服务器上
            ImageIO.write(bufferedImage, "jpg", resp.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception();
        }
    }


}
